package com.alpesressources.comexpert;

import com.alpesressources.comexpert.bo.Config;
import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.alpesressources.comexpert.bo.IObject;
import com.alpesressources.comexpert.bo.request.VoidRequest;
import com.alpesressources.comexpert.bo.response.ErrorResponse;
import com.alpesressources.comexpert.bo.response.ErrorResponse.EnumErrorCode;
import com.alpesressources.comexpert.bo.response.GetConfigResponse;
import com.alpesressources.comexpert.bo.response.Response;
import com.alpesressources.comexpert.bo.response.ResponseObject.EnumResult;
import com.alpesressources.comexpert.dao.DaoManager;
import com.alpesressources.comexpert.exception.DaoException;
import com.alpesressources.comexpert.util.MyJsonSerializer;

public class GetConfig extends AAction {

	private static Logger logger = Logger.getLogger(GetConfig.class);

	

	@Override
	public void processGet(ServletConfig config,
			HashMap<String, Object> queryString, HttpServletResponse response)
			throws ServletException, IOException {
	}

	@Override
	public void processPost(ServletConfig config,
			HashMap<String, Object> queryString, IObject postRequest,
			HttpServletResponse response) throws ServletException, IOException {

		GetConfigResponse getConfigResponse = new GetConfigResponse();

		try {
			DaoManager daoManager=new DaoManager(queryString.get("dossier").toString());
			Config conf = daoManager.getConfig();

			getConfigResponse.setResult(EnumResult.OK);
			getConfigResponse.setMySQLHost(conf.getMysqlHost());
			getConfigResponse.setMySQLUser(conf.getMysqlUser());
			getConfigResponse.setMySQLPassWord(conf.getMysqlPassword());
			getConfigResponse.setMySQLDBName(conf.getMysqlDatabaseName());
			getConfigResponse.setHTTPHost(conf.getHttpHost());
		} catch (DaoException e) {
			getConfigResponse.setResult(EnumResult.NOK);

			logger.error(e);
		}
		
		Response r = new Response();
		ErrorResponse errorResponse = new ErrorResponse();
		
		errorResponse.setErrorCode(EnumErrorCode.NO_ERROR);
		r.setErrorResponse(errorResponse);

		r.getData().add(getConfigResponse);

		MyJsonSerializer myJsonSerializer = new MyJsonSerializer();

		response.getOutputStream().write(
				myJsonSerializer.serialize(r).getBytes());
	}

	@Override
	public Class<?> getObjectClass() {
		return VoidRequest.class;
	}

}
